//-----------------------------------------------------------------------------------
#include "StateMachine.h"
//-----------------------------------------------------------------------------------
StateMachine::StateMachine()
{

}
//-----------------------------------------------------------------------------------
StateMachine::StateMachine(int stateCount, int eventCount, int initState)
{
	_fsm = new std::vector<sState*>*[stateCount];
	_drawFuncs = new DrawFuncPointer[stateCount];
	_updateFuncs = new UpdateFuncPointer[stateCount];

	for(int i = 0; i < stateCount; i++)
	{
		_drawFuncs[i] = NULL;
		_updateFuncs[i] = NULL;

		_fsm[i] = new std::vector<sState*>[eventCount];
	}

	_stateCount = stateCount;
	_state = initState;
}
//-----------------------------------------------------------------------------------
StateMachine::~StateMachine()
{
	int i = 0;
	int j = 0;
	unsigned int w = 0;

	unsigned int uiFsmSize = (unsigned int)_fsm[i][j].size();

	if(_fsm)
	{
		for(i = 0; i < _stateCount; i++)
		{
			for(j = 0; j < _stateCount; j++)
			{
				for(w = 0; w < uiFsmSize; w++)
				{
					delete _fsm[i][j][w];
				}
				
			}

			delete [] _fsm[i];
		}

		delete [] _fsm;
		delete [] _updateFuncs;
		delete [] _drawFuncs;
	}
}
//-----------------------------------------------------------------------------------
bool StateMachine::Update()
{
	if(_updateFuncs[_state])
	{
		return _updateFuncs[_state]();
	}

	return true;
}
//-----------------------------------------------------------------------------------
void StateMachine::Draw()
{
	if(_drawFuncs[_state])
	{
		_drawFuncs[_state]();
	}
}
//-----------------------------------------------------------------------------------
void StateMachine::InitializeStateMachine(int stateCount, int eventCount, int initState)
{
	_fsm = new std::vector<sState*>*[stateCount];
	_drawFuncs = new DrawFuncPointer[stateCount];
	_updateFuncs = new UpdateFuncPointer[stateCount];

	int i = 0;
	int j = 0;

	for(i = 0; i < stateCount; i++)
	{
		_drawFuncs[i] = NULL;
		_updateFuncs[i] = NULL;

		_fsm[i] = new std::vector<sState*>[eventCount];
	}

	_stateCount = stateCount;
	_state = initState;
}
//-----------------------------------------------------------------------------------
bool StateMachine::ShutDown()
{
	if(_fsm)
	{
		int i = 0;

		for(i = 0; i < _stateCount; i++)
		{
			delete [] _fsm[i];
		}

		delete [] _fsm;
		delete [] _updateFuncs;
		delete [] _drawFuncs;

		return true;
	}

	return true;
}
//-----------------------------------------------------------------------------------